﻿@{
    ViewData["Title"] = "添加";
}

<form id="myFormId" target="frmright">
    <input type="hidden" name="editType" value="@ViewData["editType"]" />
    <input type="hidden" name="partId" value="@ViewData["partId"]" />
    <input type="hidden" name="partType" id="partType" value="@ViewData["partType"]" />
    <div class="basicTab" id="myTab" style="height:460px;">
        <div name="移动单">
            <table class="tableStyle" formMode="view">
                <tr>
                    <td>单据类型</td>
                    <td>
                        <select prompt="请选择" name="orderType" id="orderType" url="/wms/MovementManage/getSelectMoveOrderType"  >
                        </select><span class="star">*</span>
                    </td>
                    <td>关联单号</td>
                    @*<td><input type="text" id="reforderno" name="reforderno" /></td>*@
                    <td>
                        <input id="refOrderNo" name="refOrderNo" value="@ViewData["refOrderNo"]" class="autoComplete" url="/wms/MovementManage/GetRefOrderNo"
                               boxwidth="150" colmulti="true" colidx="0" type="text" />
                    </td>
                </tr>
                <tr>
                    <td>计划日期</td>
                    <td> <input type="text" id="planDate" name="planDate" class="date" /><span id="planDateStar" class="star">*</span></td>
                    <td>
                        计划时间
                    </td>
                    <td>
                        <input type="text" id="planTime" name="planTime" style="width:auto" class="date" dateFmt="HH:mm:ss" /><span id="planTimeStar" class="star">*</span>
                    </td>
                </tr>
                <tr>
                    <td>源仓库</td>
                    <td><select selectedValue="@ViewData["srcWarehouseId"]" id="srcWarehouseId" url="/MovementOutstockManage/getSelectWarehouse" name="srcWarehouseId"></select><span class="star">*</span></td>
                    <td>目标仓库</td>
                    <td><select selectedValue="@ViewData["destWarehouseId"]" id="destWarehouseId" url="/MovementOutstockManage/getSelectWarehouse" name="destWarehouseId"></select><span class="star">*</span></td>
                </tr>
                <tr>
                    <td>源库区</td>
                    <td><select selectedValue="@ViewData["srcZoneId"]" id="srcZoneId" prompt="请选择" name="srcZoneId"></select></td>
                    <td>目标库区</td>
                    <td><select selectedValue="@ViewData["destZoneId"]" id="destZoneId" prompt="请选择" name="destZoneId"></select><span class="star">*</span></td>
                </tr>
                <tr>
                    <td>源库位</td>
                    <td><select selectedValue="@ViewData["srcLocateId"]" id="srcLocateId" prompt="请选择" name="srcLocateId"></select></td>
                    <td>目标库位</td>
                    <td><select selectedValue="@ViewData["destLocateId"]" id="destLocateId" prompt="请选择" name="destLocateId"></select><span class="star">*</span></td>
                </tr>
                <tr>
                    <td>源库存地</td>
                    <td><input type="text" id="srcErpWarehouse" name="srcErpWarehouse" disabled="disabled" /></td>
                    <td>目标库存地</td>
                    <td><input type="text" id="destErpWarehouse" name="destErpWarehouse" disabled="disabled" /></td>
                </tr>
                <tr>
                    <td>备注</td>
                    <td><input type="text" id="remarks" name="remarks" /></td>
                    <td>是否可用</td>
                    <td><select selectedValue="@ViewData["enabled"]" name="enabled" id="enabled" data='{"list":[{"value":"Y","key":"是"},{"value":"N","key":"否"}]}'></select></td>
                </tr>
                <tr  >
                <td  class="hiddenBL" >&nbsp;&nbsp;补料类型：</td>
                <td   class="hiddenBL">
                    <select prompt="请选择" url="/wms/MovementManage/getSelectFedbatchType" name="fedbatchType" id="fedbatchType" >
                    </select>
                </td>
                <td   class="hiddenBL" >&nbsp;&nbsp;补料方式：</td>
                <td   class="hiddenBL">
                        <select prompt="请选择" url="/wms/MovementManage/getSelectFedbatchMethods" name="fedbatchMethods" id="fedbatchMethods">
                    </select>
                </td>
                </tr>
            </table>
        </div>
        <div name="移动单明细">
            <table class="tableStyle" formMode="view">
                <tr>
                    <td>零件号</td>
                    <td>
                        <input id="partNo" name="partNo" value="@ViewData["partNo"]" class="autoComplete" url="/wms/MovementManage/GetPart_No"
                               boxwidth="150" colmulti="true" colidx="0" type="text" />
                    </td>
                    <td>简码</td>
                    <td>
                        <input id="partSpec" name="partSpec" value="@ViewData["partSpec"]" class="autoComplete" url="/wms/MovementManage/GetPart_Spec"
                               boxwidth="150" colmulti="true" colidx="0" type="text" />
                    </td>
                <tr>
                    <td>数量</td>
                    <td><input type="text" name="qty" value="@ViewData["qty"]" id="qty" /></td>
                    <td>箱数</td>
                    <td><input type="text" name="boxQty" value="@ViewData["boxQty"]" id="boxQty" /></td>
                </tr>
            </table>
            <input type="button" value="添加" onclick="return onAdd()" />&nbsp;&nbsp;<input type="button" value="删除" onclick="ondelete()" />
            <div id="maingrid"></div>
        </div>
    </div>
    <div>
        <table class="tableStyle" formMode="transparent">
            <tr>
                <td colspan="2">
                    <input type="submit" value="提交" onclick="return add()" />
                    <input type="button" value="取消" onclick="top.Dialog.close()" />
                </td>
            </tr>
        </table>
    </div>
</form>
<!-- 异步提交start -->
<script type="text/javascript">
    var grid;
    var indexVal = 0;
    function initComplete() {
        debugger
        var srcWarehouseId = '@ViewData["srcWarehouseId"]';
        if (srcWarehouseId != "" && srcWarehouseId != null) {
            $.post("/LocateDefine/getSelectZone", { "WarehouseId": srcWarehouseId }, function(result) {
                //赋给data
                $("#srcZoneId").data("data", result)
                //刷新下拉框
                $("#srcZoneId").render();
            }, "json");
        }
        var destWarehouseId = '@ViewData["destWarehouseId"]';
        if (destWarehouseId != "" && destWarehouseId != null) {
            $.post("/LocateDefine/getSelectZone", { "WarehouseId": destWarehouseId }, function(result) {
                //赋给data
                $("#destZoneId").data("data", result)
                //刷新下拉框
                $("#destZoneId").render();
            }, "json");
        }


        var date = new Date(); //获取完整的日期
        var year = date.getFullYear(); //获取当前年份
        var month = date.getMonth() + 1;//获取当前月份
        var day = date.getDate();//获取当前日
        month = (month > 9) ? month : ("0" + month);
        day = (day < 10) ? ("0" + day) : day;
        var today = year + "-" + month + "-" + day;
        $("#planDate").val(today);

        var hour = date.getHours(); //获取 时
        var minutes = date.getMinutes(); //获取 分
        var seconds = date.getSeconds() //获取 秒
        var timeStr = hour + ":" + minutes + ":" + seconds;
        $("#planTime").val(timeStr);

        grid = $("#maingrid").quiGrid({
            columns: [
                { display: '零件号', name: 'partNo', align: 'center', width: "33%" },
                { display: '简码', name: 'partSpec', align: 'center', width: "34%" },
                { display: '数量', name: 'qty', align: 'center', width: "33%" }
            ],
            data: [], pageSize: 10, rownumbers: true, checkbox: true, percentWidthMode: true, sortName: "partNo", sortOrder: "desc",
            height: '300', width: "100%",
        });

        $("#myTab").bind("actived", function(e, idx) {
            if (idx == 1) {
                grid.resetHeight();
                grid.resetWidth();
            }
        })

        $("#srcWarehouseId").bind("change", function() {
            var WarehouseId = $("#srcWarehouseId").val();
            $.post("/LocateDefine/getSelectZone", { "WarehouseId": WarehouseId }, function(result) {
                //赋给data
                $("#srcZoneId").data("data", result)
                //刷新下拉框
                $("#srcZoneId").render();
            }, "json");
        })

        $("#destWarehouseId").bind("change", function() {
            var WarehouseId = $("#srcWarehouseId").val();
            $.post("/LocateDefine/getSelectZone", { "WarehouseId": WarehouseId }, function (result) {
                //赋给data
                $("#destZoneId").data("data", result)
                //刷新下拉框
                $("#destZoneId").render();
            }, "json");
        })

        $("#srcZoneId").bind("change", function() {
            debugger
            var zoneId = $("#srcZoneId").val();
            //erp库存地
            $.post("/wms/MovementManage/getSelectZoneByZoneId", { "zoneId": zoneId }, function (result) {
                //赋给data
                $("#srcErpWarehouse").val(result[0].erpWarehouse)
            }, "json");

            if (zoneId != "" && zoneId != null) {
                $.post("/wms/MovementManage/getSelectLocate", { "zoneId": zoneId }, function (result) {
                    //赋给data
                    $("#srcLocateId").data("data", result)
                    //刷新下拉框
                    $("#srcLocateId").render();
                }, "json");
            }

        })

        $("#destZoneId").bind("change", function() {
            debugger
            var zoneId = $("#destZoneId").val();
            //erp库存地
            $.post("/wms/MovementManage/getSelectZoneByZoneId", { "zoneId": zoneId }, function (result) {
                //赋给data
                $("#destErpWarehouse").val(result[0].erpWarehouse)
            }, "json");

            if (zoneId != "" && zoneId != null) {
                $.post("/wms/MovementManage/getSelectLocate", { "zoneId": zoneId }, function (result) {
                    //赋给data
                    $("#destLocateId").data("data", result)
                    //刷新下拉框
                    $("#destLocateId").render();
                }, "json");
            }

        })

        $("#refOrderNo").bind("change", function () {
            var refOrderNo = $("#refOrderNo").val();
        })

        $("#partNo").bind("change", function() {
            var partNo = $("#partNo").val();
            //if (partNo != "" && partNo != null) {
            //    $("#partSpec").val("");
            //    return;
            //}
        })

        $("#partSpec").bind("change", function() {
            var partSpec = $("#partSpec").val();
            //if (partSpec != "" && partSpec != null) {
            //    $("#partNo").val("");
            //    return;
            //}
        })

        $("#qty").bind("change", function() {
            var qty = $("#qty").val();
            //if (qty != "" && qty != null) {
            //    $("#boxQty").val("");
            //    return;
            //}
        })

        $("#boxQty").bind("change", function() {
            var boxQty = $("#boxQty").val();
            //if (boxQty != "" && boxQty != null) {
            //    $("#qty").val("");
            //    return;
            //}
        })

        //表单提交
        $('#myFormId').submit(function() {

            $.post("/wms/MovementManage/saveMovementManage",
                {
                    "fedbatchType": $("#fedbatchType").val(), "enabled": $("#enabled").val(),
                    "fedbatchMethods": $("#fedbatchMethods").val(), "enabled": $("#enabled").val(),
                    "orderType": $("#orderType").val(), "enabled": $("#enabled").val(),
                    "planDate": $("#planDate").val(), "planTime": $("#planTime").val(),
                    "destWarehouseId": $("#destWarehouseId").val(), "destZoneId": $("#destZoneId").val(),
                    "destErpWarehouse": $("#destErpWarehouse").val(),
                    "srcWarehouseId": $("#srcWarehouseId").val(), "srcZoneId": $("#srcZoneId").val(),
                    "srcErpWarehouse": $("#srcErpWarehouse").val(),
                    "srcLocateId": $("#srcLocateId").val(),
                    "destLocateId": $("#destLocateId").val(),
                    "remarks": $("#remarks").val(),
                    "orderno": $("#orderno").val(),
                    "refOrderNo": $("#refOrderNo").val(),
                    "detailRow": JSON.stringify(grid.rows)
                },
                function(result) {
                    top.Dialog.alert(result.message, function() {
                        closeWin();
                    })
                });

            return false;
        });

        $("#orderType").bind("change", function() {
            debugger
            var orderType = $("#orderType").val();
            if (orderType == "10") {//生产领料单
                document.getElementsByClassName("hiddenBL")[0].style.display = "none";
                document.getElementsByClassName("hiddenBL")[1].style.display = "none";
                document.getElementsByClassName("hiddenBL")[2].style.display = "none";
                document.getElementsByClassName("hiddenBL")[3].style.display = "none";
                document.getElementById("srcWarehouseId").disabled = true;
                document.getElementById("srcZoneId").attr("disabled", "disabled");

                document.getElementById("destWarehouseId").disabled = false;
                document.getElementById("destZoneId").attr("disabled", "disabled");  
            }
            else if (orderType == "40") {//NC入库单
                document.getElementsByClassName("hiddenBL")[0].style.display = "none";
                document.getElementsByClassName("hiddenBL")[1].style.display = "none";
                document.getElementsByClassName("hiddenBL")[2].style.display = "none";
                document.getElementsByClassName("hiddenBL")[3].style.display = "none";
                document.getElementById("srcWarehouseId").disabled = false;
                document.getElementById("srcZoneId").attr("disabled", "disabled");

                document.getElementById("destWarehouseId").disabled = false;
                document.getElementById("destZoneId").attr("disabled", "disabled");
            }
            else if (orderType == "50") {//NC让步接收单
                document.getElementsByClassName("hiddenBL")[0].style.display = "none";
                document.getElementsByClassName("hiddenBL")[1].style.display = "none";
                document.getElementsByClassName("hiddenBL")[2].style.display = "none";
                document.getElementsByClassName("hiddenBL")[3].style.display = "none";
                document.getElementById("srcWarehouseId").disabled = false;
                document.getElementById("srcZoneId").attr("disabled", "disabled");

                document.getElementById("destWarehouseId").disabled = false;
                document.getElementById("destZoneId").attr("disabled", "disabled");           
            }
            else if (orderType == "140") {//单据移库单
                document.getElementsByClassName("hiddenBL")[0].style.display = "none";
                document.getElementsByClassName("hiddenBL")[1].style.display = "none";
                document.getElementsByClassName("hiddenBL")[2].style.display = "none";
                document.getElementsByClassName("hiddenBL")[3].style.display = "none";
                document.getElementById("srcWarehouseId").disabled = false;
                document.getElementById("srcZoneId").attr("disabled", "disabled");

                document.getElementById("destWarehouseId").disabled = false;
                document.getElementById("destZoneId").attr("disabled", "disabled");          
            }
            else if (orderType == "300") {//生产补料单
                document.getElementsByClassName("hiddenBL")[0].style.display = "";
                document.getElementsByClassName("hiddenBL")[1].style.display = "";
                document.getElementsByClassName("hiddenBL")[2].style.display = "";
                document.getElementsByClassName("hiddenBL")[3].style.display = "";
            }
            else if (orderType == "280") {//生产退料单
                document.getElementsByClassName("hiddenBL")[0].style.display = "";
                document.getElementsByClassName("hiddenBL")[1].style.display = "";
                document.getElementsByClassName("hiddenBL")[2].style.display = "";
                document.getElementsByClassName("hiddenBL")[3].style.display = "";
            }
            else {
                document.getElementsByClassName("hiddenBL")[0].style.display = "none";
                document.getElementsByClassName("hiddenBL")[1].style.display = "none";
                document.getElementsByClassName("hiddenBL")[2].style.display = "none";
                document.getElementsByClassName("hiddenBL")[3].style.display = "none";
                document.getElementById("srcWarehouseId").disabled = false;
                document.getElementById("srcZoneId").attr("disabled", "disabled");

                document.getElementById("destWarehouseId").disabled = false;
                document.getElementById("destZoneId").attr("disabled", "disabled");             
            }
            grid.loadData();
        });

    }
    var detailList = [];
    function onAdd() {
        var partNo = $("#partNo").val();
        var partSpec = $("#partSpec").val();
        if ((partNo == "" || partNo == null) && (partSpec == "" || partSpec == null)) {
            alert("请选择零件号或简码！");
            return;
        }

        var boxQty = $("#boxQty").val()
        var qty = $("#qty").val()
        if ((qty == "" || qty == null) && (boxQty == "" || boxQty == null)) {
            alert("请输入数量或箱数！");
            return;
        }

        //输入零件号或简码
        if (partNo != "" && partNo != null) {
            var partNo = $("#partNo").val();
            $.post("/wms/MovementManage/getPartNoInfoByPartNo", { "partNo": partNo }, function(result) {
                debugger
                if (result.length == 0) {
                    alert("零件号不存在，请检查！");
                    return;
                }
                let gridArr = grid.rows;
                for (let i = 0, len = gridArr.length; i < len; i++) {
                    if (gridArr[i].partNo == partNo)//判断存在不添加
                    {
                        alert("零件号已添加，请检查！");
                        return;
                    }
                }
                var sumQty = 0;
                if (qty == "" || qty == null) {
                    if (result[0].defaultBoxQty == 0) {
                        alert("零件号未维护包装数量，请输入数量！");
                        return;
                    }
                    sumQty = parseInt(result[0].defaultBoxQty) * parseInt(boxQty);
                }
                else {
                    sumQty = parseInt($("#qty").val());
                }

                detailList.push({
                    index: indexVal,
                    partNo: result[0].partNo,
                    partSpec: result[0].partSpec,
                    qty: sumQty,
                });

                var detailListPush = [];
                detailListPush.push({
                    rows: detailList
                })

                grid.loadData(detailListPush[0]);
                indexVal++;

                
                $("#partNo").val("");
                $("#partSpec").val("");
                $("#boxQty").val("");
                $("#qty").val("");

            }, "json");
        }
        else if (partSpec != "" && partSpec != null) {
            var partSpec = $("#partSpec").val();
            $.post("/wms/MovementManage/getPartSpecInfoByPartSpec", { "partSpec": partSpec }, function (result) {
                debugger
                if (result.length == 0) {
                    alert("零件号不存在，请检查！");
                    return;
                }
                let gridArr = grid.rows;
                for (let i = 0, len = gridArr.length; i < len; i++) {
                    if (gridArr[i].partNo == result[0].partNo)//判断存在不添加
                    {
                        alert("零件号已添加，请检查！");
                        return;
                    }
                }

                var sumQty = 0;
                if (qty == "" || qty == null) {
                    if (result[0].defaultBoxQty == 0) {
                        alert("零件号未维护包装数量，请输入数量！");
                        return;
                    }
                    sumQty = parseInt(result[0].defaultBoxQty) * parseInt(boxQty);
                }
                else {
                    sumQty = parseInt($("#qty").val());
                }

                detailList.push({
                    index: indexVal,
                    partNo: result[0].partNo,
                    partSpec: result[0].partSpec,
                    qty: sumQty,
                });

                var detailListPush = [];
                detailListPush.push({
                    rows: detailList
                })

                grid.loadData(detailListPush[0]);
                indexVal++;

            }, "json");
        }

    }

    function ondelete() {
        var selectedRows = grid.getSelectedRows();
        var selectedRowsLength = selectedRows.length;
        var ids = "";
        debugger
        for (var i = 0; i < selectedRowsLength; i++) {
            grid.deleteRow(selectedRows[i]);
            grid.loadData({ rows: grid.rows });
        }

    }

    //判断用户输入是否正确
    function add() {
        var orderType = $("#orderType").val();
        if (orderType == "" || orderType == null) {
            alert("请选择单据类型！");
            return false;
        }

        var planDate = $("#planDate").val()
        if (planDate == "" || planDate == null) {
            alert("请输入计划日期！");
            return false;
        }

        var planTime = $("#planTime").val()
        if (planTime == "" || planTime == null) {
            alert("请输入计划时间！");
            return false;
        }

        var srcWarehouseId = $("#srcWarehouseId").val()
        if (srcWarehouseId == "" || srcWarehouseId == null) {
            alert("请选择源仓库！");
            return false;
        }

        //var srcZoneId = $("#srcZoneId").val()
        //if (srcZoneId == "" || srcZoneId == null) {
        //    alert("请选择源库区！");
        //    return false;
        //}

        var destWarehouseId = $("#destWarehouseId").val()
        if (destWarehouseId == "" || destWarehouseId == null) {
            alert("请选择目标仓库！");
            return false;
        }

        var destZoneId = $("#destZoneId").val()
        if (destZoneId == "" || destZoneId == null) {
            alert("请选择目标库区！");
            return false;
        }

        var destLocateId = $("#destLocateId").val()
        if (destLocateId == "" || destLocateId == null) {
            alert("请选择目标库位！");
            return false;
        }

        var rowNum = grid.getData().length;
        if (rowNum == 0) {
            alert("请生成移动单明细！");
            return false;
        }



        //输入关联单号
        if (refOrderNo != "" && refOrderNo != null) {
            var refOrderNo = $("#refOrderNo").val();
            $.post("/wms/MovementManage/getPartNoInfoByPartNo", { "refOrderNo": refOrderNo }, function (result) {
                debugger
                if (result.length == 0) {
                    alert("单号不存在，请检查！");
                    return;
                }
                let gridArr = grid.rows;
                for (let i = 0, len = gridArr.length; i < len; i++) {
                    if (gridArr[i].refOrderNo == refOrderNo)//判断存在不添加
                    {
                        alert("零件号已添加，请检查！");
                        return;
                    }
                }
                var sumQty = 0;
                if (qty == "" || qty == null) {
                    if (result[0].defaultBoxQty == 0) {
                        alert("零件号未维护包装数量，请输入数量！");
                        return;
                    }
                    sumQty = parseInt(result[0].defaultBoxQty) * parseInt(boxQty);
                }
                else {
                    sumQty = parseInt($("#qty").val());
                }

                detailList.push({
                    index: indexVal,
                    refOrderNo: result[0].refOrderNo,
                    partSpec: result[0].partSpec,
                    qty: sumQty,
                });

                var detailListPush = [];
                detailListPush.push({
                    rows: detailList
                })

                grid.loadData(detailListPush[0]);
                indexVal++;

            }, "json");
        }






        return true;
    } 
    //重置
    function closeWin() {
        debugger
        //刷新数据
        var tabfrmId = "page_" + top.frmright.getCurrentTabId();
        top.frmright.document.getElementById(tabfrmId).contentWindow.refresh(false);
        //关闭窗口
        top.Dialog.close();
    }
</script>
<!-- 异步提交end -->	